package nmp.lib;
import java.util.ArrayList;

import nmp.lib.CNmpCmd.NmpCmdBase;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;

public class CNmpJsonCmd implements CNmpCmd {

	private int m_session_num = 0;
	
	public String response_identify_str;
	public String response_wifilist_str;
	public String response_wificonnect_str;
	public String response_wifidisconnect_str;
	public String response_deviceinfo_str;
	public String response_wificonfig_str;
	
	public CNmpJsonCmd(){
		response_identify_str = "{\"id\":1, \"session\":8423,\"command\":\"identify\",\"name\":\"ALi phone\",\"version\": 1000}";
		response_wifilist_str = "{\"id\":2,\"session\":8344,\"command\":\"wifilist\",\"list\":";
		response_wifilist_str += "[{\"ssid\":\"jericho\",\"protect\":\"WPA2\",\"strength\":5,\"status\":0},"; 
		response_wifilist_str += "{\"ssid\":\"walace\" ,\"protect\":\"WPA\" ,\"strength\":3,\"status\":1},"; 
		response_wifilist_str += "{\"ssid\":\"xavier\" ,\"protect\":\"WPA\" ,\"strength\":2,\"status\":0}]}";
		response_wificonnect_str = "{\"id\":3,\"session\":5527,\"command\":\"wificonnect\"}";
		response_wifidisconnect_str = "{\"id\":4,\"session\":132,\"command\":\"wifidisconnect\"}";
		response_deviceinfo_str = "\"id\":5,\"session\":6232,\"command\":\"deviceinfo\",\"ssid\":\"jericho\",\"password\":\"123dascesf\",\"name\":\"PESI M3912\",";
		response_deviceinfo_str +="\"mac\": \"0090e6000008\",\"majorver\":1\"minorver\":10";
		response_wificonfig_str = "{\"id\":6,\"session\":8953,\"command\":\"wificonfig\"}";
	}

	public NmpCmdBase Parse(String jsonstr) {
		int id = -1;
		
		//JSONArray jsonArray = null;
		JSONObject jsonObject = null;
		try {
			jsonObject = (JSONObject) new JSONTokener(jsonstr).nextValue();
			//jsonArray = new JSONArray(jsonstr);
			//jsonObject = jsonArray.getJSONObject(0);
			id = jsonObject.getInt("id");
		} catch (JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
			return null;
		}		
		
		switch(id) {
			case NMP_CMD_ID_IDENTIFY:
				return (NmpCmdBase)IdentifyParse(jsonObject);
				
			case NMP_CMD_ID_WIFILIST:
				return (NmpCmdBase)WifiListParse(jsonObject);

			case NMP_CMD_ID_WIFICONNECT:
				return (NmpCmdBase)WifiConnectParse(jsonObject);
		
			case NMP_CMD_ID_WIFIDISCONNECT:
				return (NmpCmdBase)WifiDisconnectParse(jsonObject);
				
			case NMP_CMD_ID_DEVICEINFO:
				return (NmpCmdBase)DeviceInfoParse(jsonObject);
				
			case NMP_CMD_ID_WIFICONFIG:				
				return (NmpCmdBase)WifiConfigParse(jsonObject);		
				
			case NMP_CMD_ID_WIFIISCONNECTED:				
				return (NmpCmdBase)WifiIsConnectedParse(jsonObject);
			default:
				break;
		}
		return null;
	} 

	/*
	Phone -> Device
	{
	  "id":1,
	  "session":8423,
	  "command":"identify",
	  "name":"ALi phone",
	  "version": 1000
	}
	*/
	public String IdentifyRequestGenerate(String name,int version) {
		String jsonString;
		//JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
        try {
        	jsonObject.put("id", NMP_CMD_ID_IDENTIFY);
        	jsonObject.put("session", m_session_num++);  
			jsonObject.put("command", "identify");
	        jsonObject.put("name",name);
	        jsonObject.put("version",version);			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //jsonArray.put(jsonObject);
        jsonString = jsonObject.toString();
        return jsonString;
	} 
	
	/*
	Phone <- Device
	{
	  "id":1,
	  "session":8423,
	  "command":"identify",  
	  "name":"ALi device",
	  "version": 1000
	}
	*/	
	private NmpCmdIdentifyInfo IdentifyParse(JSONObject jsonObject) {
		try {
			NmpCmdIdentifyInfo info = new NmpCmdIdentifyInfo();
			info.id = jsonObject.getInt("id");
			info.session = jsonObject.getInt("session"); 
			info.command = jsonObject.getString("command");			
			info.version = jsonObject.getInt("version");
			return info;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}			
	}	

	/*
	Phone -> Device
	{
	  "id":2,
	  "session":8344,
	  "command":"wifilist"  
	}
	 */
	public String WifiListRequestGenerate() {
		String jsonString;
		//JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
        try {
        	jsonObject.put("id", NMP_CMD_ID_WIFILIST);
        	jsonObject.put("session", m_session_num++);  
			jsonObject.put("command", "wifilist");	
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //jsonArray.put(jsonObject);
        jsonString = jsonObject.toString();
        return jsonString;
	} 	
	
	/*
	Phone <-- Device
	{
	  "id":2,
	  "session":8344,
	  "command":"wifilist",
	  "list":
	  [
	   {"ssid":"jericho","protect":"WPA2","strength":5,"status":0},
	   {"ssid":"walace" ,"protect":"WPA" ,"strength":5,"status":1},
	   {"ssid":"xavier" ,"protect":"WPA" ,"strength":5,"status":1}
	  ]
	}
	// strength range:[1~5]
	// status 0:disconnect 1:connected.
	 */
	private NmpCmdWifiList WifiListParse(JSONObject jsonObject) {
		
		try {
			NmpCmdWifiList info = new NmpCmdWifiList();
			info.id = jsonObject.getInt("id");
			info.session = jsonObject.getInt("session"); 
			info.command = jsonObject.getString("command");
			JSONArray jsonlist = jsonObject.getJSONArray("list");

			info.list = new ArrayList<NmpWifiInfo>();
			int index=0;
			
			while(true) {
				JSONObject jsonObj;
				try {
					jsonObj = jsonlist.getJSONObject(index);
				}
				catch (JSONException e) {
					jsonObj = null;
					break;
				}
				NmpWifiInfo wifiInfo = new NmpWifiInfo();				
				wifiInfo.ssid = jsonObj.getString("ssid");
				wifiInfo.protect = jsonObj.getString("protect");
				wifiInfo.strength = jsonObj.getInt("strength");
				wifiInfo.status = jsonObj.getInt("status");				
				info.list.add(wifiInfo);
				index++;
			}
			return info;
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}			
	}
	
	/*
	Phone --> Device
	{
	  "id":3,
	  "session":5527,
	  "command":"wificonnect",  
	  "target": {"ssid":"jericho","protect":"WPA2","password":"stringpassowrd"}
	}
	 */
	public String WifiConnectRequestGenerate(String ssid, String protect,String password) {
		String jsonString;
		//JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
        try {
        	jsonObject.put("id", NMP_CMD_ID_WIFICONNECT);
        	jsonObject.put("session", m_session_num++); 
			jsonObject.put("command", "wificonnect");	
			JSONObject TargetObject = new JSONObject();
			TargetObject.put("ssid",ssid);
			TargetObject.put("protect",protect);
			TargetObject.put("password",password);
			jsonObject.put("target", TargetObject);	
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //jsonArray.put(jsonObject);
        jsonString = jsonObject.toString();
        return jsonString;
	} 	
	
	/*
	Phone <-- Device
	{
	  "id":3,
	  "session":5527,
	  "command":"wificonnect"
	}
	*/
	
	private NmpCmdBase WifiConnectParse(JSONObject jsonObject) {
		NmpCmdBase info = new NmpCmdBase();
		try {
			info.id = jsonObject.getInt("id");
			info.session = jsonObject.getInt("session"); 
			info.command = jsonObject.getString("command");		
			return info;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}		
	}
	
/*
	Phone --> Device
	{
	  "id":4,
	  "session":132,
	  "command":"wifidisconnect"
	}	
 */
	public String WifiDisconnectRequestGenerate() {
		String jsonString;
		//JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
        try {
        	jsonObject.put("id", NMP_CMD_ID_WIFIDISCONNECT);
        	jsonObject.put("session", m_session_num++); 
			jsonObject.put("command", "wifidisconnect");	
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //jsonArray.put(jsonObject);
        jsonString = jsonObject.toString();
        return jsonString;
	} 	
	
	/*
	Phone <-- Device
	{
	  "id":4,
	  "session":132,
	  "command":"wifidisconnect"
	}
	*/
	
	private NmpCmdBase WifiDisconnectParse(JSONObject jsonObject) {
		NmpCmdBase info = new NmpCmdBase();
		try {
			info.id = jsonObject.getInt("id");
			info.session = jsonObject.getInt("session"); 
			info.command = jsonObject.getString("command");		
			return info;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}		
	}
	
/*
 *  Phone --> Device
	{
	  "id":5,
	  "session":6232,
	  "command":"deviceinfo"
	}	
 */
	
	public String DeviceInfoRequestGenerate() {
		String jsonString;
		//JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
        try {
        	jsonObject.put("id", NMP_CMD_ID_DEVICEINFO);
        	jsonObject.put("session", m_session_num++); 
			jsonObject.put("command", "deviceinfo");	
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //jsonArray.put(jsonObject);
        jsonString = jsonObject.toString();
        return jsonString;
	} 	

/*
 Phone <-- Device
{
	"id":5,
	"session":6232,
	"command":"deviceinfo",
	"ssid":"jericho",
	"password":"123dascesf" 
	"name":"PESI M3912",
	"mac": "0090e6000008",     
	"majorver":1
	"minorver":10
}	
 */
	
	private NmpCmdDeviceInfo DeviceInfoParse(JSONObject jsonObject) {
		try {
			NmpCmdDeviceInfo info = new NmpCmdDeviceInfo();
			info.id = jsonObject.getInt("id");
			info.session = jsonObject.getInt("session"); 
			info.command = jsonObject.getString("command");		
			info.ssid = jsonObject.getString("ssid");
			info.mac = jsonObject.getString("mac");
			info.password = jsonObject.getString("password");
			info.name = jsonObject.getString("name");
			info.firmware_majon = jsonObject.getInt("majorver");
			info.firmware_minor = jsonObject.getInt("minorver");				
			return info;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}			
	}	

	/*
	Phone --> Device
	{
	  "id":6,
	  "session":8953,
	  "command":"wificonfig",
	  "ssid":"jericho"
      "protect":"WPA2"
      "password":"stringpassowrd"
	}
	 */
	public String WifiConfigRequestGenerate(String ssid, String protect,String password) {
		String jsonString;
		//JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
        try {
        	
        	jsonObject.put("id", NMP_CMD_ID_WIFICONFIG);
			jsonObject.put("command", "wificonfig");
			jsonObject.put("session", m_session_num++); 
			jsonObject.put("ssid",ssid);
			jsonObject.put("protect",protect);
			jsonObject.put("password",password);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //jsonArray.put(jsonObject);
        jsonString = jsonObject.toString();
        return jsonString;
	} 	
/*
 * Phone <-- Device
	{
	  "id":6,
	  "session":8953,
	  "command":"wificonfig",
	}	
 */
	private NmpCmdBase WifiConfigParse(JSONObject jsonObject) {
		NmpCmdBase info = new NmpCmdBase();
		try {
			info.id = jsonObject.getInt("id");
			info.session = jsonObject.getInt("session"); 
			info.command = jsonObject.getString("command");		
			return info;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}		
	}	
	
	/*
	    Phone --> Device
		{
		  "id":7,
		  "session":6212,
		  "command":"wifisconnected",
		}	
	 */	
	
	public String WifiIsConnectedRequestGenerate() {
		String jsonString;
		//JSONArray jsonArray = new JSONArray();
		JSONObject jsonObject = new JSONObject();
        try {
        	
        	jsonObject.put("id", NMP_CMD_ID_WIFIISCONNECTED);
			jsonObject.put("command", "wifiisconnected");
			jsonObject.put("session", m_session_num++);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        //jsonArray.put(jsonObject);
        jsonString = jsonObject.toString();
        return jsonString;
	} 	
	/*
    Phone --> Device
	{
	  "id":7,
	  "session":6212,
	  "command":"wifisconnected",
	  "isconnected":0
	}	
 */		
	private NmpCmdBase WifiIsConnectedParse(JSONObject jsonObject) {
		NmpCmdIsConnected info = new NmpCmdIsConnected();
		try {
			info.id = jsonObject.getInt("id");
			info.session = jsonObject.getInt("session"); 
			info.command = jsonObject.getString("command");		
			info.isconnected = jsonObject.getInt("isconnected"); 
			return info;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}		
	}	
};
